extends Node2D

onready var anim = $AnimationPlayer
onready var sensor: Area2D = $"omg!/Sensor"
onready var omg: RigidBody2D = $"omg!"
onready var timer: Timer = $Timer2

var bodies = []

func _process(delta: float) -> void:
	omg.angular_velocity = -omg.rotation * 10
	
	bodies = sensor.get_overlapping_bodies()
	if not bodies.empty():
		var player: RigidBody2D = bodies[0]
		if timer.time_left > 0:
			omg.linear_velocity = (player.global_position - omg.global_position) * delta * 200
		else:
			omg.linear_velocity.x = (player.global_position.x - omg.global_position.x) * delta * 50


func _on_Timer_timeout() -> void:
	if not bodies.empty():
		timer.start()


func _on_omg_body_entered(body: Node) -> void:
	if "子弹" in body.name:
		anim.play("dead", -1, 3.0)
		yield(anim, "animation_finished")
		queue_free()
